import { reqAssessAdd } from "../../api/assess"
import {upload} from "../../api/common"
import { toast } from "../../utils/extendApi";

// pages/assess/assess.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    anonymouschecked:false,
    star: 3,
    orderId:'',
    content:'',
    imageList:[],
  },

  onInput: function(e) {
    this.setData({
      content: e.detail.value
    });
  },

  onLoad(options){
    // console.log(options)
    if(options.orderId){
      this.setData({
        orderId:options.orderId
      })
    }
  },

  readimage(e){
    // console.log(e.detail.file.url)
    const url = e.detail.file.url
    const image = {
      url:url,
      isImage:true
    }
    const list = this.data.imageList
    list.push(image)
    this.setData({
      imageList:list
    })
  },

  deleteimage(e){
    // console.log(e.detail.index)
    let arr = this.data.imageList
    let index = e.detail.index
    arr.splice(index,1)
    this.setData({
      imageList:arr
    })
  },

  back(){
    wx.navigateBack()
  },

  onchangestar(event){
    this.setData({
      star: event.detail,
    });
  },

  onchangeanonymouschecked(event){
    this.setData({
      anonymouschecked:event.detail
    })
  },

  async assess(){
    let arr = this.data.imageList;
    let list =[]
    for (let i = 0; i < arr.length; i++) {
      const {data} = await upload(arr[i].url)
      list.push(data)
    }
    console.log(list)

    const param={
      imageList:list,
      orderId:this.data.orderId,
      stars:this.data.star,
      assessContent:this.data.content,
      isAnonymous:this.data.anonymouschecked

    }

    const {code} = await reqAssessAdd(param)

    if (code){
      this.back()
      wx.toast({
        title:'评价成功',
        icon:'success'
      })
    }
    else{
      wx.toast({
        title:'评价失败',
        icon:'error'
      })
    }
  },

  
})